import Vue from 'vue';
import store from '@/store';
/* export const clickOutside = {
	//自定义指令：clickOutside
	bind(el, bindings, vnode) {
		//自定义指令生命周期：bind ；参数：el, bindings, vnode
		let handler = e => {
			e.stopPropagation();
			bindings.value(el, el === e.target);
		};
		el.handler = handler;
		document.addEventListener('click', handler);
	},
	unbind(el) {
		document.removeEventListener('click', el.handler);
	}
}
export const nodeConfig = {
	bind(el, bindings, vnode) {
		//创建div
		el.addEventListener('mouseover', e => {
			e.stopPropagation();
			el.style.setProperty('border-color', '#f0f');
		})
		el.addEventListener('mouseout', e => {
			e.stopPropagation();
			el.removeAttribute('style');
		})
	},
	unbind(el) {
		el.removeEventListener('mouseenter', () => {})
		el.removeEventListener('mouseout', () => {})
	}
} */
export const removeAriaHidden = {
	bind(el, binding) {
		let ariaEls = el.querySelectorAll('.el-radio__original');
		ariaEls.forEach((item) => {
			item.removeAttribute('aria-hidden');
		});
	}
}

export const htmlDescript = {
	bind(el, bindings, vnode) {
		let content = document.createElement('div');
		//let text = Vue.prototype.$cleanHtmlTag(bindings.value);
		content.className = 'descont';
		if (!bindings.value) {
			content.innerHTML = '无';
		} else {
			let html = Vue.prototype.$substringHtml(bindings.value, 80);
			let button = document.createElement('button');
			button.innerText = '展开';
			button.setAttribute('hand', false);
			content.innerHTML = html;
			button.addEventListener('click', (e) => {
				let isUnfold = JSON.parse(e.target.getAttribute('hand'));
				button.setAttribute('hand', !isUnfold);
				button.innerText = !isUnfold ? '收起' : '展开';
				content.innerHTML = !isUnfold ? bindings.value : html;
			})
			el.appendChild(button);
		}
		el.appendChild(content);
	},
	inserted(el, bindings) {},
	update(el, bindings, vnode) {
		let content = el.getElementsByClassName('descont')[0];
		let button = el.getElementsByTagName('button')[0];
		if (!bindings.value) return false;
		content.innerHTML = Vue.prototype.$substringHtml(bindings.value, 80);
	},
	componentUpdated(el) {}
}

//按钮权限指令
export const hasPermi = {
	inserted(el, bindings, vnode) {
		let value = bindings.value;
		let authButton = store.getters.authButton;
		if (!value || !authButton.some(item => item.id === value)) {
			el.parentNode?.removeChild(el);
		}
	},

	update(el, bindings) {
		let value = bindings.value;
		let authButton = store.getters.authButton;
		if (!!bindings.value && authButton.some(item => item.id === value)) {
			if (!el.parentNode) {
				el.__v_originalParent.insertBefore(el, el.__v_anchor || null);
			}
		} else {
			if (el.parentNode) {
				el.__v_anchor = document.createComment("");
				el.__v_originalParent = el.parentNode;
				el.parentNode.replaceChild(el.__v_anchor, el);
			}
		}
	}
}